import java.io.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-06-18
 * Time: 12:16
 */

/**
 * 对于字符流,只能操作文本文件,不能处理非文本文件
 * 对于字节流,通常来处理非文本文件,但是如果涉及到文本文件的复制操作,也能用;如果涉及到读取,含有中文的时候,可能会出现乱码
 *
 */

/**
 * 文本文件: .txt .java .c .cpp .py
 * 非文本文件: .doc  .xls  .jpg .png .pdf .mp3. mp4 .avi ....
 */
public class FileInputOutputStreamTest {
    /**
     * 复制一份hello.txt文件,命名为hello1_copy.txt
     *
     */
    public static void main(String[] args){
        //1.创建File类对象
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File src = new File("hello.txt");
            File dest = new File("hello1_copy.txt");
            //2.创建相关的字节流
            fileInputStream = new FileInputStream(src);
            fileOutputStream = new FileOutputStream(dest);
            //3.数据的读写
            byte[] buff = new byte[5];//1kb
            int len;//记录每次读入到buff中的字节个数
            while ((len = fileInputStream.read(buff)) != -1) {
                fileOutputStream.write(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileInputStream != null;
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fileOutputStream != null;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //4.关闭流资源

    }
    /**
     * 复制一份test.jpg文件,命名为test_copy.jpg
     *
     */
    public static void main2(String[] args){
        //1.创建File类对象
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            File src = new File("test.jpg");
            File dest = new File("test_copy.jpg");
            //2.创建相关的字节流
            fileInputStream = new FileInputStream(src);
            fileOutputStream = new FileOutputStream(dest);
            //3.数据的读写
            byte[] buff = new byte[1024];//1kb
            int len;//记录每次读入到buff中的字节个数
            while ((len = fileInputStream.read(buff)) != -1) {
                fileOutputStream.write(buff, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                assert fileInputStream != null;
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                assert fileOutputStream != null;
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //4.关闭流资源

    }
    /**
     * 复制一份test.jpg文件,命名为test_copy.jpg
     *
     * 复制失败,因为字符流不是适合用来处理非文本文件
     * @param args
     */
    public static void main1(String[] args) throws IOException {
        //1. 创建File类的对象
        FileReader fileReader = null;

        FileWriter fileWriter = null;
        try {
            File file = new File("test.jpg");
            File dest = new File("test_copy.jpg");
            //2. 创建输入流输出流
            fileReader = new FileReader(file);
            fileWriter = new FileWriter(dest);
            //3. 数据的读入和写出
            char[] buff = new char[5];
            int len;
            while ((len = fileReader.read(buff)) != -1) {
                fileWriter.write(buff, 0, len);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4. 关闭流资源
            assert fileReader != null;
            fileReader.close();
            assert fileWriter != null;
            fileWriter.close();

        }

    }
}
